﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using Test;

namespace WebForm {
    public partial class Form1 : Form {
        private List<Company> lstCompanies; 
        private const string SEARCH_URL =
            "http://www.yellowpages.com.au/search/listings?clue={0}&locationClue=All+States&lat=&lon=&pageNumber={1}";
        public Form1() {
            InitializeComponent();
            webBrowser1.ScriptErrorsSuppressed = true;
            lstCompanies = new List<Company>();
        }

        private void button1_Click(object sender, EventArgs e) {
            var url = string.Format(SEARCH_URL, m_txtCategory.Text, m_txtPage.Text);
            m_txtAddress.Text =url;//"http://www.yellowpages.com.au/search/listings?clue=Acupuncture&locationClue=&lat=&lon=";
            label4.Text = "[Loading....]";
            webBrowser1.Navigate(url);
            while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) {
                Application.DoEvents();
            }
            label4.Text = "Finished...";
        }
        private void SaveResult() {
           
           
            //var page2 = site.Search("Acupuncture", cate);
            //companies.AddRange(page2);
            var workbook = new HSSFWorkbook();
            var sheet = workbook.CreateSheet("Acupuncture");
            var headerRow = sheet.CreateRow(0);
            headerRow.CreateCell(0).SetCellValue("Categories");
            headerRow.CreateCell(1).SetCellValue("Business name");
            headerRow.CreateCell(2).SetCellValue("Address");
            headerRow.CreateCell(3).SetCellValue("City");
            headerRow.CreateCell(4).SetCellValue("State");
            headerRow.CreateCell(5).SetCellValue("PostCode");
            headerRow.CreateCell(6).SetCellValue("Phone");
            headerRow.CreateCell(7).SetCellValue("Email");
            headerRow.CreateCell(8).SetCellValue("Website");
            for (int i = 0; i < lstCompanies.Count; i++) {
                var company = lstCompanies[i];
                var row = sheet.CreateRow(i + 1);
                row.CreateCell(0).SetCellValue(company.Category);
                row.CreateCell(1).SetCellValue(company.BussinessName);
                row.CreateCell(2).SetCellValue(company.StreetAddress);
                row.CreateCell(3).SetCellValue(company.City);
                row.CreateCell(4).SetCellValue(company.State);
                row.CreateCell(5).SetCellValue(company.PostCode);
                row.CreateCell(6).SetCellValue(company.Phone1);
                row.CreateCell(7).SetCellValue(company.Email);
                row.CreateCell(8).SetCellValue(company.Website);
            }
            using (var file = new FileStream("ouput.xls", FileMode.Create)) {
                workbook.Write(file);
                file.Flush();
                file.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e) {
           SaveResult();
        }

        private void button3_Click(object sender, EventArgs e) {
            var document = webBrowser1.DocumentText;
            AuYellowPage site = new AuYellowPage();
            List<Company> companies = site.Search(document, m_txtCategory.Text);
            lstCompanies.AddRange(companies);
            MessageBox.Show("Analyze finished");
        }
    }
}
